#include "link.h"

// 创建链表
link_node * link_create(void)
{
    link_node *L = NULL;
        
    L = (link_node *)malloc(sizeof(link_node));
    
    L->data = 0;
    L->next = NULL;

    return L;
}

// 任意位置插入
int pos_insert(link_node *L, int pos, int value){
    assert(L != NULL);

    int i = 1;
    link_node *p = L->next;

    while(p != NULL){
        p = p->next;
        i++;
    }

    if (pos > i || pos < 1) {
        printf("位置超出链表范围");
        return -1;
    }

    link_node *tmp = (link_node *)malloc(sizeof(link_node));
    tmp->data = value;
    tmp->next = NULL;

    p = L;

    for (int i = 1; i < pos; i++) {
        p = p->next;
    }

    tmp->next = p->next;
    p->next = tmp;

    return 0;
}

// 删除数据
int link_node_delete(link_node *L, int pos){
    assert(L != NULL);

    int i = 1;
    link_node *p = L->next;

    while(p != NULL){
        p = p->next;
        i++;
    }

    if (pos > i || pos < 1) {
        printf("位置超出链表范围");
        return -1;
    }

    p = L;

    for (i=1; i<pos; i++) {
        p = p->next;
    }

    link_node *del = p->next;
    p->next = del->next;
    free(del);

    return 0;
}

// 遍历链表
int link_list_show(link_node *L){

    if (L == NULL) {
        printf("链表为空\n");
        return -1;
    }

    link_node *p = L;

    printf("self               next          \t data\n");
    while (p->next != NULL)
    {
        printf("%p -> %14p \t %4d\n",p, p->next, p->data);
        p = p->next;
    }

    printf("%p -> %14p \t %4d\n",p, p->next, p->data);

    puts("");

    return 0;
}


// 释放链表
int link_list_free(link_node **L){

    link_node *p = (*L)->next;
    
    while ((*L)->next != NULL)
    {
        printf("释放： %p\n", (*L));
        p = (*L)->next;
        free(*L);
        (*L) = p;
    }

    printf("释放： %p\n", (*L));
    free(*L);
    *L = NULL;

    return 0;
}

// 根据位置获取节点
link_node * link_node_get(link_node *L, int pos) {

    link_node *p = L->next;

    int i = 1;

    while (p->next != NULL) {
        if (i == pos) {
            return p;
        }
        i++;
        p = p->next;
    }

    return NULL;
}

// 根据数据查找


// 链表翻转
int link_node_reverse(link_node *L) {
    link_node *first = L->next;
    link_node *L0 = first->next; 
    first->next = NULL;

    while (L0->next != NULL) {
        link_node *tmp = L0;
        L0 = L0->next;
        tmp->next = L->next;
        L->next = tmp;
    }

    L0->next = L->next;
    L->next = L0;

    return 0;
}


// 链表合并
int link_list_merge(link_node *L, link_node *L1)
{
    link_node *p = L->next;
    while (p->next != NULL) {
        p = p->next;
    }

    p->next = L1->next;

    return 0;
}

// 链表排序
int link_list_sort(link_node *L) {
    assert(L != NULL);

    int total = 0;
    link_node *p = L->next;

    while(p != NULL){
        p = p->next;
        total++;
    }

    p = L->next;
    int i = 0;
    int j = 0;

    link_node *outer = L->next;

    for (i = 0; i < total; i++) {
        link_node *inner = L->next;
        for (j = 0; j < total; j++) {
            int tmp = outer->data;

            if (outer->data < inner->data) {
                outer->data = inner->data;
                inner->data = tmp;
            }

            inner = inner->next;
        }

        outer = outer->next;
    }

    return 0;
}





